﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core;
using com.woodsoft.core.Files;
using EQ1.Elements;
using System.Xml;
using EQ1.Objects;

namespace EQ1.Elements {
  public class TemplateScreen : Control , IUIElement<TemplateScreen> {
    public TemplateScreen ( UIDocument doc , string name )
      : base( doc , name ) {
    }
    public TemplateScreen ( UIDocument doc )
      : this( doc , "TemplateScreen" ) {
    }

    #region IUIElement<TemplateScreen> Members

    public void Validate ( TemplateScreen val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        TemplateScreen el = new TemplateScreen( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }
    public void Copy ( TemplateScreen val ) {
      base.Copy( val );

      if( this.Templates != null )
        this.Templates = val.Templates;
    }

    #endregion

    public XmlNodeList Templates {
      get {
        return this.GetElementsByTagName( "Templates" );
      }
      set {
        foreach( UITemplateAssoc nd in value ) {
          this.AddTemplate( nd );
        }
      }
    }
    public UITemplateAssoc GetTemplate ( string name ) {
      foreach( UITemplateAssoc nd in this.Templates ) {
        if( nd.Name == name ) {
          return nd;
        }
      }

      return null;
    }
    public void AddTemplate ( UITemplateAssoc val ) {
      for( int i = this.Templates.Count - 1 ; i >= 0 ; i-- ) {
        if( ((UITemplateAssoc)this.Templates[i]).Name == val.Name ) {
          this.ReplaceChild( val , this.Templates[i] );
          return;
        }
      }

      this.AppendChild( val );
    }
  }
}
